// pages/voidDetails/voidDetails.js
import api from '../../utils/api';

Page({

  /**
   * 页面的初始数据
   */
  data: {
    uuid: '',
    coId: '',
    id: '',
    voidDetailsList: '',
    show: false,
    fpcode: '',
    invDateStr: '',
    cancelReasonType: '',
    cancelledDateStr: '',
    reason: '',
    remarks: '',
    submitTime: '',
    passed_active: 0,
    showtab2: false, //作废审批流程
    zanwu: false,
    voidflowList: [], //作废审批流程
    isExaminer: '', //审批意见显示与否
    editableCancel: '', //底部按钮 
    cancelReasonTypeStr: '', //作废原因类型
    //审批
    fixed: true,
    message: '',
    shenpishow: false,
    flowId: '', // 流程ID
    minDate: '',
    maxDate: '',
    //作废原因
    show: false, //作废原因弹框
    cancelReasonTypeStr: '', //作废原因-类型
    dictKey: '', //类型对应的key
    dictValue02: '', // 原因
  },
  //发票作废--补充信息跳转
  buchong(e) {
     console.log("发票作废--补充信息跳转",e.currentTarget.dataset.from)
    wx.navigateTo({
      url: '/pages/suppInformation/suppInformation?id= ' + this.data.id + '&code=' + this.data.voidDetailsList.code + '&deptName=' + this.data.voidDetailsList.opDeptName + '&userName=' + this.data.voidDetailsList.operatorName + '&from=' + e.currentTarget.dataset.from + '&voucherCode=' + this.data.voidDetailsList.voucherCode + '&voucherDate=' + this.data.voidDetailsList.voucherDate + '&voucherOp=' + this.data.voidDetailsList.voucherOp
    })
  },
  // 输入审批意见
  onshenpiInput() {
    this.setData({
      shenpishow: true
    })
  },
  //  关闭弹窗
  onshenpiClose() {
    this.setData({
      shenpishow: false
    })
  },
  onshenpiChange(e) {
    console.log(e)
    this.setData({
      message: e.detail
    })
  },
  // 拒绝
  onDecline() {
    let that = this;
    console.log(that.data.flowId, "1111")
    api.voidjujue({
      data: {
        invId: that.data.id,
        flowId: that.data.flowId,
        flowNotes: that.data.message,
        userId: wx.getStorageSync('userInfo').unitId
      },
      success: function (res) {
        console.log(res)
        if (res.data.code == 200) {
          wx.showToast({
            title: '审批成功',
          })
          setTimeout(() => {
            wx.navigateBack({
              delta: 1,
            })
          }, 100);
        } else {
          wx.showModal({
            content: res.data.message,
            showCancel: false,
            confirmColor: '#0041C4'
          })
        }
      }
    })
  },
  // 同意
  onAgree() {
    let that = this;
    api.voidtongyi({
      data: {
        invId: that.data.id,
        flowId: that.data.flowId,
        flowNotes: that.data.message,
        userId: wx.getStorageSync('userInfo').unitId
      },
      success: function (res) {
        console.log(res)
        if (res.data.code == 200) {
          wx.showToast({
            title: '审批成功',
          })
          setTimeout(() => {
            wx.navigateBack({
              delta: 1,
            })
          }, 100);
        } else {
          wx.showModal({
            content: res.data.message,
            showCancel: false,
            confirmColor: '#0041C4'
          })
        }
      }
    })
  },
  formatDate(date) {
    date = new Date(date);
    return `${date.getFullYear()}/${date.getMonth() + 1}/${date.getDate()}`;
  },
  formatDate(date) {
    date = new Date(date);
    return `${date.getFullYear()}-${date.getMonth() + 1}-${date.getDate()}`;
  },
  //日期弹框1
  onDisplay(e) {
    this.setData({
      minDate: new Date(2018, 0, 1).getTime(),
      maxDate: new Date(2028, 0, 31).getTime(),
      show: true
    });
  },
  onClose() {
    this.setData({
      show: false
    });
  },

  onConfirm(event) {
    this.setData({
      show: false,
      invDateStr: this.formatDate(event.detail),
    });
  },
  //日期弹框2
  onDisplay2(e) {
    this.setData({
      minDate: new Date(2018, 0, 1).getTime(),
      maxDate: new Date(2028, 0, 31).getTime(),
      show2: true
    });
  },
  onClose2() {
    this.setData({
      show2: false
    });
  },

  onConfirm2(event) {
    this.setData({
      show2: false,
      cancelledDateStr: this.formatDate(event.detail),
    });
  },

  //修改
  onfpCode(e) {
    console.log(e.detail, "fpcode")
    this.setData({
      fpcode: e.detail
    })
  },
  //作废原因-类型 修改
  onviodType(event) {
    console.log(event, "作废原因类型")
    this.setData({
      showType: true
    });
    this.voidReason()

  },
  //作废原因类型列表初始渲染
  voidReason() {
    let that = this;
    api.getCancelReasonTypeList({
      data: {
        coId: that.data.coId,
      },
      success: function (res) {
        console.log(res.data.data, "voidReason")
        if (res.data.code = 200) {
          that.data.columns = [];
          if (res.data.data.length > 0) {
            res.data.data.forEach((item) => {
              let obj = {
                text: item.dictValue,
                dictKey: item.dictKey,
                dictValue02: item.dictValue02,
              }
              that.data.columns.push(obj)
              // console.log(that.data.columns, 'oooo')
            });
            that.setData({
              columns: that.data.columns,
            })
            that.setData({
              showType: true
            })
          } else {
            that.setData({
              showType: false
            })
          }

        }
      }
    })
  },
  onCancelType() {
    this.setData({
      showType: false
    });
  },
  onConfirmType(e) {
    const {
      value,
    } = e.detail;
    console.log(value, "val")
    let text = value.text;
    let dictKey = value.dictKey;
    let dictValue02 = value.dictValue02;
    this.data.dictKey = dictKey;
    this.data.dictValue02 = dictValue02;
    this.setData({
      reason: this.data.dictValue02, //作废原因
      cancelReasonTypeStr: value.text,
      cancelReasonType: this.data.dictKey,
      showType: false,
    })
  },
  //原因
  onreason(e) {
    // console.log(e.detail, "onreason")
    this.setData({
      reason: e.detail
    })
  },
  onremarks(e) {
    // console.log(e.detail, "onremarks")
    this.setData({
      remarks: e.detail
    })
  },
  //获取数据
  voidDetails() {
    console.log(this.data.id, 'iddd')
    let that = this
    api.getInvCancelDetail({
      data: {
        id: this.data.id,
      },
      success: function (res) {
        console.log(res.data.data, "getInvCancelDetail ")
        if (res.data.code == 200) {
          res.data.data.invDateStr = res.data.data.invDateStr ? res.data.data.invDateStr.toString().trim().split(' ')[0] :'',
          res.data.data.cancelledDateStr = res.data.data.cancelledDateStr ? res.data.data.cancelledDateStr.toString().trim().split(' ')[0] :'',
          res.data.data.submitTime = res.data.data.submitTime ? res.data.data.submitTime.toString().trim().split(' ')[0] :'',
          res.data.data.money = that.addComma(res.data.data.money)
          that.setData({
            uuid: res.data.data.uuid,
            coId: res.data.data.coId,
            voidDetailsList: res.data.data,
            invDateStr: res.data.data.invDateStr,
            cancelledDateStr: res.data.data.cancelledDateStr,
            fpCode: res.data.data.fpCode,
            reason: res.data.data.reason,
            remarks: res.data.data.remarks,
            submitTime: res.data.data.submitTime,
            isExaminer: res.data.data.isExaminer,
            editableCancel: res.data.data.editableCancel,
            cancelReasonType: res.data.data.cancelReasonType,
            cancelReasonTypeStr: res.data.data.cancelReasonTypeStr, //作废原因类型
            voucherCode:res.data.data.voucherCode,
            voucherDate:res.data.data.voucherDate,
            voucherOp:res.data.data.voucherOp,
          })
        }
      }
    })
  },
  //切换选项卡
  onChange(event) {
    console.log(event.detail, '1111')
    if (event.detail.index == 0) {
      this.voidDetails()
    } else {
      this.getInvCancelProcess()
    }
  },
  //保存
  save() {
    // console.log(this.data.cancelReasonType)
    let that = this
    api.getInvCancelupdate({
      // code
      method: 'post',
      data: {
        id: this.data.id,
        invCode: that.data.fpCode,
        invDate: new Date(this.data.invDateStr).getTime(),
        cancelledDate: new Date(this.data.cancelledDateStr).getTime(),
        reason: this.data.reason,
        cancelReasonType: this.data.cancelReasonType,
        remarks: this.data.remarks,
      },
      success: function (res) {
        console.log(res.data.data, "getInvCancelupdate")
        if (res.data.code == 200) {
          wx.showToast({
            title: '保存成功',
            icon: 'success'
          })
        }
      }
    })
  },
  //提交
  submit() {
    // this.save() //自动保存
    let that = this
    api.getInvCancelupdate({
      // code
      method: 'post',
      data: {
        id: this.data.id,
        invCode: that.data.fpCode,
        invDate: new Date(this.data.invDateStr).getTime(),
        cancelledDate: new Date(this.data.cancelledDateStr).getTime(),
        reason: this.data.reason,
        cancelReasonType: this.data.cancelReasonType,
        remarks: this.data.remarks,
      },
      success: function (res) {
        console.log(res.data.data, "getInvCancelupdate")
        if (res.data.code == 200) {
          wx.showToast({
            title: '保存成功',
            icon: 'success'
          })
          api.invCancelSubmitApproval({
            method: 'post',
            data: {
              uuid: that.data.uuid,
              invId: this.data.id,
            },
            success: function (res) {
              console.log(res.data.data, "invCancelSubmitApproval")
              if (res.data.code == 200) {
                wx.showToast({
                  title: '提交成功',
                  icon: 'success'
                })
                that.voidDetails()
                that.setData({
                  showtab2: true,
                  passed_active: 0
                });
              } else {
                  wx.showModal({
                    content: res.data.message,
                    showCancel: false,
                    confirmColor: '#0041C4'
                  })      
              }
            }
          })
        }
      }
    })
  },
  //再次提交
  resSubmit() {
    // this.save() //自动保存

    let that = this

    api.getInvCancelupdate({
      // code
      method: 'post',
      data: {
        id: this.data.id,
        invCode: that.data.fpCode,
        invDate: new Date(this.data.invDateStr).getTime(),
        cancelledDate: new Date(this.data.cancelledDateStr).getTime(),
        reason: this.data.reason,
        cancelReasonType: this.data.cancelReasonType,
        remarks: this.data.remarks,
      },
      success: function (res) {
        console.log(res.data.data, "getInvCancelupdate")
        if (res.data.code == 200) {
          wx.showToast({
            title: '保存成功',
            icon: 'success'
          })
          api.invCancelResSubmitApproval({
            method: 'post',
            data: {
              uuid: that.data.uuid,
              invId: this.data.id,
            },
            success: function (res) {
              console.log(res.data.data, "invCancelResSubmitApproval")
              if (res.data.code == 200) {
                setTimeout(() => {
                 wx.showToast({
                  title: '提交成功',
                  icon: 'success'
                })
                }, 100);
                that.voidDetails()
                that.setData({
                  showtab2: true,
                  passed_active: 0
                });
              } else {
                  wx.showModal({
                    content: res.data.message,
                    showCancel: false,
                    confirmColor: '#0041C4'
                  })
              }
            }
          })
        }
      }
    })
   
    
  },

  //作废审批流程
  getInvCancelProcess() {
    let that = this
    api.getInvCancelProcess({
      data: {
        uuid: that.data.uuid,
      },
      success: function (res) {
        console.log(res.data.data, "getInvCancelProcess")
        if (res.data.code == 200) {
          for (let i = 0; i < res.data.data.length; i++) {
            console.log(res.data.data[i].flowStepStatus, "ssssssss")
            if (res.data.data[i].flowStepStatus == null) {
              that.data.flowId = res.data.data[i].flowId
            }
          }
          if (res.data.data.length > 0) {
            that.setData({
              voidflowList: res.data.data,
              flowId: that.data.flowId
            })
          } else {
            console.log(res.data.data.length, "lerngth")
            that.setData({
              zanwu: true
            })
          }
        }
      }
    })
  },
  //千分位 转换
  addComma(date) {
    let str = '';
    let num = '';
    let newStr = '';
    date = date + '';
    if (date.indexOf('.') != -1) {
      num = date.split('.')[0]
      str = date.split('.')[1]
    } else {
      num = date
    }
    newStr = num.toString().replace(/(\d)(?=(?:\d{3})+$)/g, '$1,');
    newStr = str == '' ? newStr : newStr + '.' + str;
    return newStr;
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options)
    this.setData({
      // uuid:options.uuid,
      id: options.id,
    })

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.voidDetails()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})